package com.invengo.library_rfid.analyze.utils.uhf.TIAOMHZH;

import java.util.regex.Pattern;

public class CBookIDRule_430 {
    public static long BookIDToEPC(String szBarCode, int nLibraryCode) {
        if (nLibraryCode >= 4011) {
            long lBookID = 0;
            switch (nLibraryCode) {
                case 4011:
                    lBookID = Convert_JXZYJSXY(szBarCode);
                    break;
                case 4012:
                    lBookID = Convert_HJZZGZBZZWHZXTSG(szBarCode);
                    break;
                case 4013:
                    lBookID = Convert_SLSTSG(szBarCode);
                    break;
                case 4014:
                    lBookID = Convert_BJWJZDZQDSZD(szBarCode);
                    break;
                case 4015:
                    lBookID = Convert_DKDX(szBarCode);
                    break;
                case 4017:
                    lBookID = Convert_JLGCZYXY(szBarCode);
                    break;
                case 4101:
                    lBookID = BookIDSwitch_HZSTSG(szBarCode);
                    break;
            }
            return lBookID;
        } else {
            long lBookID = NormalRule_STOL(szBarCode);
            if (lBookID != 0) {
                return lBookID;
            } else {
                switch (nLibraryCode) {
                    case 4002:
                        lBookID = STOL_BJHKHTDXNBCXYJY(szBarCode);
                        break;
                }
                return lBookID;
            }
        }
    }

    public static String EPCToBookID(long lBarCode, int nLibraryCode) {
        if (nLibraryCode >= 4011) {
            String szBookID = "";
            switch (nLibraryCode) {
                case 4011:
                    szBookID = Convert_JXZYJSXY(lBarCode);
                    break;
                case 4012:
                    szBookID = Convert_HJZZGZBZZWHZXTSG(lBarCode);
                    break;
                case 4014:
                    szBookID = Convert_BJWJZDZQDSZD(lBarCode);
                    break;
                case 4015:
                    szBookID = Convert_DKDX(lBarCode);
                    break;
                case 4017:
                    szBookID = Convert_JLGCZYXY(lBarCode);
                    break;
                case 4101:
                    szBookID = BookIDSwitch_HZSTSG(lBarCode);
                    break;
            }
            return szBookID;
        } else {
            String szBookID = NormalRule_LTOS(lBarCode);
            if (!szBookID.equals("")) {
                return szBookID;
            } else {
                switch (nLibraryCode) {
                    case 4002:
                        szBookID = LTOS_BJHKHTDXNBCXYJY(lBarCode);
                        break;
                }
                return szBookID;
            }
        }
    }

    public static long NormalRule_STOL(String szBarCode) {
        if (szBarCode.length() >= 1 && szBarCode.length() <= 12 && Pattern.matches("\\d+", szBarCode)) {
            return Long.parseLong("1" + szBarCode);
        } else if (szBarCode.length() >= 1 && szBarCode.length() <= 10
                && Character.isLetter(szBarCode.charAt(0))
                && Pattern.matches("\\d+", szBarCode.substring(1))) {
            return Long.parseLong("2" + String.format("%03d", (int) szBarCode.charAt(0)) + szBarCode.substring(1));
        } else if (szBarCode.length() >= 2 && szBarCode.length() <= 10
                && Character.isLetter(szBarCode.charAt(0)) && Character.isLetter(szBarCode.charAt(1))
                && Pattern.matches("\\d+", szBarCode.substring(2))) {
            return Long.parseLong("3" + String.format("%03d", (int) szBarCode.charAt(0))
                    + String.format("%03d", (int) szBarCode.charAt(1)) + szBarCode.substring(2));
        } else if (szBarCode.length() >= 3 && szBarCode.length() <= 6
                && Character.isLetter(szBarCode.charAt(0)) && Character.isLetter(szBarCode.charAt(1))
                && Character.isLetter(szBarCode.charAt(2))
                && Pattern.matches("\\d+", szBarCode.substring(3))) {
            return Long.parseLong("4" + String.format("%03d", (int) szBarCode.charAt(0))
                    + String.format("%03d", (int) szBarCode.charAt(1))
                    + String.format("%03d", (int) szBarCode.charAt(2))
                    + szBarCode.substring(3));
        } else {
            return 0;
        }
    }

    public static String NormalRule_LTOS(long lBarCode) {
        String szBookID = "";
        String szTemp = Long.toString(lBarCode);
        if (szTemp.charAt(0) == '1') {
            return szTemp.substring(1);
        } else if (szTemp.charAt(0) == '2') {
            return Character.toString((char) Integer.parseInt(szTemp.substring(1, 4))) + szTemp.substring(4);
        } else if (szTemp.charAt(0) == '3') {
            return Character.toString((char) Integer.parseInt(szTemp.substring(1, 4)))
                    + Character.toString((char) Integer.parseInt(szTemp.substring(4, 7))) + szTemp.substring(7);
        } else if (szTemp.charAt(0) == '4') {
            return Character.toString((char) Integer.parseInt(szTemp.substring(1, 4)))
                    + Character.toString((char) Integer.parseInt(szTemp.substring(4, 7)))
                    + Character.toString((char) Integer.parseInt(szTemp.substring(7, 10))) + szTemp.substring(10);
        } else {
            return szBookID;
        }
    }

    private static long STOL_BJHKHTDXNBCXYJY(String szBarCode) {
        long lBookID = 0;
        try {
            if (szBarCode.length() == 14 && szBarCode.substring(0, 4).equals("NLIC")
                    && Pattern.matches("\\d+", szBarCode.substring(4))) {
                lBookID = Long.parseLong("5" + szBarCode.substring(4));
            }
            return lBookID;
        } catch (Exception e) {
            return 0;
        }
    }

    private static String LTOS_BJHKHTDXNBCXYJY(long lBarCode) {
        String szBookID = "";
        try {
            String szTemp = Long.toString(lBarCode);
            if (szTemp.charAt(0) == '5' && szTemp.length() == 11) {
                szBookID = "NLIC" + szTemp.substring(1);
            }
            return szBookID;
        } catch (Exception e) {
            return szBookID;
        }
    }

    private static long Convert_JXZYJSXY(String szBarCode) {
        long lBookID = 0;
        try {
            if (szBarCode.length() == 8 && Character.isLetter(szBarCode.charAt(0)) && szBarCode.charAt(0) >= 'A'
                    && szBarCode.charAt(0) <= 'Z' && Pattern.matches("\\d+", szBarCode.substring(1))) {
                lBookID = Long.parseLong("2" + String.format("%03d", (int) szBarCode.charAt(0)) + szBarCode.substring(1));
            }
            return lBookID;
        } catch (Exception e) {
            return 0;
        }
    }

    private static String Convert_JXZYJSXY(long lBarCode) {
        String szBookID = "";
        try {
            String szTemp = Long.toString(lBarCode);
            if (Long.toString(lBarCode).charAt(0) == '2') {
                return Character.toString((char) Integer.parseInt(Long.toString(lBarCode).substring(1, 4)))
                        + Long.toString(lBarCode).substring(4);
            }
            return szBookID;
        } catch (Exception e) {
            return szBookID;
        }
    }

    private static long Convert_HJZZGZBZZWHZXTSG(String szBarCode) {
        long lBookID = 0;
        try {
            if (szBarCode.length() == 6 && Pattern.matches("\\d+", szBarCode)) {
                return Long.parseLong("1" + szBarCode);
            }
            return lBookID;
        } catch (Exception e) {
            return 0;
        }
    }
    public static String Convert_HJZZGZBZZWHZXTSG(long lBarCode) {
        String szBookID = "";
        try {
            if (Long.toString(lBarCode).charAt(0) == '1') {
                return Long.toString(lBarCode).substring(1);
            }
            return szBookID;
        } catch (Exception e) {
            return szBookID;
        }
    }

    public static long Convert_SLSTSG(String szBarCode) {
        long lBookID = 0;
        try {
            if (szBarCode.length() == 7 && Pattern.matches("\\d+", szBarCode)) {
                return Long.parseLong("1" + szBarCode);
            }
            return lBookID;
        } catch (Exception e) {
            return 0;
        }
    }

    public static String Convert_SLSTSG(long lBarCode) {
        String szBookID = "";
        try {
            if (Long.toString(lBarCode).charAt(0) == '1') {
                return Long.toString(lBarCode).substring(1);
            }
            return szBookID;
        } catch (Exception e) {
            return szBookID;
        }
    }

    public static long Convert_BJWJZDZQDSZD(String szBarCode) {
        long lBookID = 0;
        try {
            if (szBarCode.length() == 7 && Pattern.matches("\\d+", szBarCode)) {
                return Long.parseLong("1" + szBarCode);
            }
            return lBookID;
        } catch (Exception e) {
            return 0;
        }
    }

    public static String Convert_BJWJZDZQDSZD(long lBarCode) {
        String szBookID = "";
        try {
            if (Long.toString(lBarCode).charAt(0) == '1') {
                return Long.toString(lBarCode).substring(1);
            }
            return szBookID;
        } catch (Exception e) {
            return szBookID;
        }
    }

    public static long Convert_DKDX(String szBarCode) {
        long lBookID = 0;
        try {
            if (szBarCode.length() == 10 && Character.isLetter(szBarCode.charAt(0))
                    && (Character.isLetter(szBarCode.charAt(9)) || szBarCode.charAt(9) == '.')
                    && Pattern.matches("\\d+", szBarCode.substring(1, 9))) {
                return Long.parseLong("5" + (int) szBarCode.charAt(0)
                        + (int) szBarCode.charAt(9)
                        + szBarCode.substring(1, 9));
            } else if (szBarCode.length() == 10 && Character.isLetter(szBarCode.charAt(0))
                    && Pattern.matches("\\d+", szBarCode.substring(1))) {
                return Long.parseLong("6" + (int) szBarCode.charAt(0) + szBarCode.substring(1));
            }
            if (szBarCode.length() == 10 && Character.isLetter(szBarCode.charAt(0))
                    && szBarCode.charAt(9) == '-'
                    && Pattern.matches("\\d+", szBarCode.substring(1, 9))) {
                return Long.parseLong("7" + (int) szBarCode.charAt(0) + szBarCode.substring(1, 9));
            }
            if (szBarCode.length() == 10 && Character.isLetter(szBarCode.charAt(0))
                    && szBarCode.charAt(9) == '.'
                    && Pattern.matches("\\d+", szBarCode.substring(1, 9))) {
                return Long.parseLong("8" + (int) szBarCode.charAt(0) + szBarCode.substring(1, 9));
            }
            return lBookID;
        } catch (Exception e) {
            return 0;
        }
    }

    public static String Convert_DKDX(long lBarCode) {
        String szBookID = "";
        try {
            if (Long.toString(lBarCode).charAt(0) == '5') {
                return Character.toString((char) Integer.parseInt(Long.toString(lBarCode).substring(1, 3)))
                        + Long.toString(lBarCode).substring(5)
                        + Character.toString((char) Integer.parseInt(Long.toString(lBarCode).substring(3, 5)));
            } else if (Long.toString(lBarCode).charAt(0) == '6') {
                return Character.toString((char) Integer.parseInt(Long.toString(lBarCode).substring(1, 3)))
                        + Long.toString(lBarCode).substring(3);
            } else if (Long.toString(lBarCode).charAt(0) == '7') {
                return Character.toString((char) Integer.parseInt(Long.toString(lBarCode).substring(1, 3)))
                        + Long.toString(lBarCode).substring(3) + "-";
            } else if (Long.toString(lBarCode).charAt(0) == '8') {
                return Character.toString((char) Integer.parseInt(Long.toString(lBarCode).substring(1, 3)))
                        + Long.toString(lBarCode).substring(3) + ".";
            }
            return szBookID;
        } catch (Exception e) {
            return szBookID;
        }
    }

    public static long Convert_JLGCZYXY(String szBarCode) {
        long lBookID = 0;
        try {
            if (szBarCode.length() >= 1 && szBarCode.length() <= 10
                    && Character.isLetter(szBarCode.charAt(0))
                    && Pattern.matches("\\d+", szBarCode.substring(1))) {
                return Long.parseLong("1" + String.format("%03d", (int) szBarCode.charAt(0)) + szBarCode.substring(1));
            }
            return lBookID;
        } catch (Exception e) {
            return 0;
        }
    }

    public static String Convert_JLGCZYXY(long lBarCode) {
        String szBookID = "";
        try {
            if (Long.toString(lBarCode).charAt(0) == '1') {
                return Character.toString((char) Integer.parseInt(Long.toString(lBarCode).substring(1, 4)))
                        + Long.toString(lBarCode).substring(4);
            }
            return szBookID;
        } catch (Exception e) {
            return szBookID;
        }
    }

    public static long BookIDSwitch_HZSTSG(String szBookID) {
        long lBookID = 0;
        try {
            if (szBookID.length() == 8 && szBookID.toUpperCase().substring(0, 2).equals("YC")
                    && Pattern.matches("\\d+", szBookID.substring(2))) {
                lBookID = Long.parseLong("3" + szBookID.substring(2));
            } else if (szBookID.length() == 8 && szBookID.toUpperCase().substring(0, 3).equals("DHT")
                    && Pattern.matches("\\d+", szBookID.substring(3))) {
                lBookID = Long.parseLong("4" + szBookID.substring(3));
            } else if (szBookID.length() == 7 && szBookID.toUpperCase().substring(0, 2).equals("DY")
                    && Pattern.matches("\\d+", szBookID.substring(2))) {
                lBookID = Long.parseLong("5" + szBookID.substring(2));
            }
            return lBookID;
        } catch (Exception e) {
            return 0;
        }
    }

    public static String BookIDSwitch_HZSTSG(long lBarCode) {
        String szBookID = "";
        try {
            String szTemp = Long.toString(lBarCode);
            if (szTemp.length() == 7 && szTemp.substring(0, 1).equals("3")) {
                szBookID = "YC" + szTemp.substring(1);
            } else if (szTemp.length() == 6 && szTemp.substring(0, 1).equals("4")) {
                szBookID = "DHT" + szTemp.substring(1);
            } else if (szTemp.length() == 6 && szTemp.substring(0, 1).equals("5")) {
                szBookID = "DY" + szTemp.substring(1);
            }
            return szBookID;
        } catch (Exception e) {
            return szBookID;
        }
    }
}
